<?php

namespace think\wework\Api;

use think\wework\Abstracts\WeworkApiWithToken;
use think\wework\Message\ResponseMessageInterface;

class AppChat extends WeworkApiWithToken
{
    private static ?AppChat $instance = null;
    public static function getInstance(): AppChat
    {
        if(!self::$instance instanceof AppChat) {
            self::$instance = new self();
        }
        return self::$instance;
    }

    /**
     * 创建群聊会话
     *
     * @param array $json
     * @return array
     */
    public function create(array $json): array
    {
        return $this->httpClient->post('appchat/create', compact('json'))->toArray();
    }

    /**
     * 修改群聊会话
     *
     * @param array $json
     * @return array
     */
    public function update(array $json): array
    {
        return $this->httpClient->post('appchat/update', compact('json'))->toArray();
    }

    /**
     * 获取群聊会话
     *
     * @param string $id
     * @return array
     */
    public function get(string $id): array
    {
        $query = ['chatid' => $id];
        return $this->httpClient->get('appchat/get', compact('query'))->toArray();
    }

    /**
     * 应用推送消息
     *
     * @param string $id
     * @param ResponseMessageInterface $responseMessage
     * @param bool $safe
     * @return array
     */
    public function send(string $id, ResponseMessageInterface $responseMessage, bool $safe = false): array
    {
        $json = array_merge(['chatid' => $id], $responseMessage->formatForResponse(), ['safe' => (int)$safe]);
        return $this->httpClient->post(
            'appchat/send',
            compact('json')
        )->toArray();
    }
}
